package com.wisdytech.linkdcs.partLife.action;


import com.wisdytech.common.handler.ResourceMatchingService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.partLife.model.ModelPart;
import com.wisdytech.linkdcs.partLife.vo.ModelpartsVO;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.wisdytech.linkdcs.partLife.service.IModelPartService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  设备型号备件配置前端控制器
 * </p>
 *
 * @author chenbin
 * @since 2018-06-05
 */
@Controller
@RequestMapping("/modelPart/partConfigure")
public class ModelPartController {
	
    /**
     * 注入备件信息iPartService
     */
    @Autowired
    @Qualifier("iModelPartService")
    private IModelPartService iModelPartService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "lifeCycle/partLifeCycle/partConfigure";
    }
    
    /**
     * 分页条件查询备件配置列表信息
     * @param modelCode 设备型号
     * @param filter 分页参数
     * @return 备件配置管理列表信息
     * @throws IOException 
     */
    @RequestMapping(value = "/listModelPartByFilter/get.mvc")
    @ResponseBody	
    public StandardTableResult<ModelpartsVO> listModelPartByFilter(String modelCode,Filter filter) throws IOException{
    	PageInfo<ModelPart> modelparts =this.iModelPartService.listModelPartByFilter(modelCode,filter);
        //转换为前台结果集
        List<ModelPart> modelpartsList=modelparts.getList();
        List<ModelpartsVO> modelpartsVOList=new ArrayList<ModelpartsVO>();
        for(ModelPart modelPart:modelpartsList){
        	modelpartsVOList.add(DataTransformUtil.changeModelPartVO(modelPart));
        }
        return new StandardTableResult<ModelpartsVO>(modelparts.getTotal(),modelpartsVOList);
    }
    
    /**
     * 保存备件配置信息
     * @param part 备件对象
     * @return 保存结果集
     */
    @RequestMapping(value = "/saveOrUpdatePartConfigInfo/save.mvc")
    @ResponseBody
    public StandardResult saveOrUpdatePartConfigInfo(ModelPart modelPart) {
        if (StringUtils.isBlank(modelPart.getPkId())) {
        	iModelPartService.addPartConfigInfo(modelPart);
        }else {
        	iModelPartService.updatePartConfigInfo(modelPart);
        }
        return  StandardResult.getSuccessResult();
    }
    
    /**
     * <p>Description:查询备件配置信息</p> 
     * @param pkId 备件配置id
     * @return 备件配置信息
     */
    @RequestMapping(value = "/getPartConfigInfoById/get.mvc")
    @ResponseBody
    public StandardResult getPartConfigInfoById(String pkId){
    	ModelPart modelPart = iModelPartService.getPartConfigInfoById(pkId);
        return StandardResult.getSuccessResult(modelPart);
    }
    
    /**
     * 通过id批量删除备件配置信息
     * @param id 主键id
     * @return 删除结果集
     */
    @RequestMapping(value = "/deletePartConfigById/del.mvc")
    @ResponseBody
    public StandardResult deletePartConfigById(String pkId) {
    	iModelPartService.deletePartConfigById(pkId);
        return StandardResult.getSuccessResult();
    }
    
    
    /**
     * 通过备件id查询位置编码
     * @param search 模糊查询
     * @param partId 备件id
     * @return 备件信息对象
     */
    @RequestMapping(value = "/listlmodelPartByPartId/get.mvc")
    @ResponseBody
    public StandardResult listlmodelPartByPartId(String search,String partId,String equipCode){
    	List<ModelPart> list = iModelPartService.listlmodelPartByPartId(search,partId,equipCode);
        return StandardResult.getSuccessResult(list);
    }
}

